﻿using System;
using System.Collections.Generic;
using System.Data;
using OestsCommon.Model;
using System.Data.SqlClient;

namespace OestsDataBase
{
    /// <summary>
    /// 实训操作
    /// </summary>
    public static class TrainDAL
    {
        /// <summary>
        /// 获得所有的数据
        /// </summary>
        /// <returns></returns>
        public static List<TrainData> GetData()
        {
            string strSql = "select * from T_Train";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            List<TrainData> list = new List<TrainData>();
            foreach (DataRow dr in dt.Rows)
            {
                TrainData t = new TrainData();
                t.StudentId = Int32.Parse(dr["student_id"].ToString());
                t.TaskId = Int32.Parse(dr["task_id"].ToString());
                t.PostId = Int32.Parse(dr["post_id"].ToString());
                t.Score = Int32.Parse(dr["score"].ToString());
                t.OperationLog = LitJson.JsonMapper.ToObject<List<string>>(dr["operationlog"].ToString());
                list.Add(t);
            }
            return list;
        }

        public static List<TrainData> GetTrainDataByTaskIdOrderByScore(int taskId)
        {
            string strSql = string.Format("select * from T_Train where task_id = {0} order by score",taskId);
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            List<TrainData> list = new List<TrainData>();
            foreach (DataRow dr in dt.Rows)
            {
                TrainData t = new TrainData();
                t.StudentId = Int32.Parse(dr["student_id"].ToString());
                t.TaskId = Int32.Parse(dr["task_id"].ToString());
                t.PostId = Int32.Parse(dr["post_id"].ToString());
                t.Score = Int32.Parse(dr["score"].ToString());
                t.OperationLog = LitJson.JsonMapper.ToObject<List<string>>(dr["operationlog"].ToString());
                list.Add(t);
            }
            return list;
        }

        public static List<TrainData> GetTrainDataByStudentID(int stdId)
        {
            string strSql = string.Format("select * from T_Train Where student_id = {0}", stdId);
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) return null;
            List<TrainData> list = new List<TrainData>();
            foreach (DataRow dr in dt.Rows)
            {
                TrainData t = new TrainData();
                t.StudentId = Int32.Parse(dr["student_id"].ToString());
                t.TaskId = Int32.Parse(dr["task_id"].ToString());
                t.PostId = Int32.Parse(dr["post_id"].ToString());
                t.Score = Int32.Parse(dr["score"].ToString());
                t.OperationLog = LitJson.JsonMapper.ToObject<List<string>>(dr["operationlog"].ToString());
                list.Add(t);
            }

            return list;
        }
        private static List<TrainData> GetData(string sql)
        {
            DataTable dt = SqlHelper.ExecuteDataTable(sql);
            if (dt == null) { return null; }
            List<TrainData> list = new List<TrainData>();
            foreach (DataRow dr in dt.Rows)
            {
                TrainData t = new TrainData();
                t.StudentId = Int32.Parse(dr["student_id"].ToString());
                t.TaskId = Int32.Parse(dr["task_id"].ToString());
                t.PostId = Int32.Parse(dr["post_id"].ToString());
                t.Score = Int32.Parse(dr["score"].ToString());
                t.OperationLog = LitJson.JsonMapper.ToObject<List<string>>(dr["operationlog"].ToString());
                list.Add(t);
            }
            return list;
        }

        public static void Add(TrainData data)
        {
            string log = ""; ;
            if(data.OperationLog != null)
            {
                log = LitJson.JsonMapper.ToJson(data.OperationLog);
            }
          
            string strSql1 = string.Format("update T_Train set score={0}, operationlog='{1}' where student_id=@student and task_id=@task and post_id=@post",
              data.Score, log);
            int n = SqlHelper.ExecuteNonQuery(strSql1, new SqlParameter("@student", data.StudentId),
                new SqlParameter("@task", data.TaskId), new SqlParameter("@post", data.PostId));
            if (n == 0)
            {
                //表示没有该学生的该任务该岗位的项目  添加一条
                string strSql2 = string.Format("insert into T_Train (student_id, task_id, post_id,score,operationlog) values({0},{1},{2},{3},'{4}')",
                data.StudentId, data.TaskId, data.PostId, data.Score, log);
                SqlHelper.ExecuteNonQuery(strSql2);
            }

            //string strSql = "select id from T_Train where student_id=@student and task_id=@task and post_id=@post";
            //DataTable dt = SqlHelper.ExecuteDataTable(strSql, new SqlParameter("@student", data.StudentId),
            //    new SqlParameter("@task", data.TaskId), new SqlParameter("@post", data.PostId));

            //if (dt == null)
            //{
            //    //
            //    string strSql2 = string.Format("insert into T_Train (student_id, task_id, post_id,score,operationlog) values({0},{1},{2},{3},'{4}')",
            //    data.StudentId, data.TaskId, data.PostId, data.Score,LitJson.JsonMapper.ToJson(data.OperationLog));
            //    SqlHelper.ExecuteNonQuery(strSql);
            //}
        }
    }
}
